#include "HiFileTransferManager.h"

QMutex HiFileTransferManager::mMutex;
HiFileTransferManager* HiFileTransferManager::mInstance = nullptr;
HiFileTransferManager::HiFileTransferManager(QObject* parent)
	:QObject(parent)
{

}

HiFileTransferManager::~HiFileTransferManager()
{

}

HiFileTransferManager* HiFileTransferManager::getInstance()
{
	QMutexLocker locker(&mMutex);
	if (mInstance == nullptr)
	{
        mInstance = new HiFileTransferManager();
	}
	return mInstance;
}

HiFileTransfer* HiFileTransferManager::addFileTransfer(const QString& name)
{
	HiFileTransfer* pFileTransfer = mFileTransferMap.value(name);
	if (pFileTransfer == nullptr)
	{
        pFileTransfer = new HiFileTransfer(this);
		mFileTransferMap.insert(name, pFileTransfer);
	}
	return pFileTransfer;
}

void HiFileTransferManager::removeFileTransfer(const QString& name)
{
	HiFileTransfer* pFileTransfer = mFileTransferMap.value(name);
	if (pFileTransfer != nullptr)
    {
        mFileTransferMap.remove(name);
        delete pFileTransfer;
    }
}

HiFileTransfer* HiFileTransferManager::getFileTransfer(const QString& name) const
{
	return mFileTransferMap.value(name);
}

QList<HiFileTransfer*> HiFileTransferManager::getFileTransferList() const
{
	return mFileTransferMap.values();
}

QMap<QString, HiFileTransfer*>::const_iterator HiFileTransferManager::getFileTransferMapIter() const
{
	return mFileTransferMap.constBegin();
}

HiFileTransferManager* getFileTransferManager()
{
	return HiFileTransferManager::getInstance();
}